#include "upperlimbwidget.h"
#include "touchbutton.h"

#include <QApplication>
#include <QButtonGroup>
#include <QEvent>
#include <QFrame>
#include <QGridLayout>
#include <QPushButton>

#include <models/databasemanager.h>
#include <models/robotlist.h>
#include <models/syssettingmodel.h>

UpperLimbWidget::UpperLimbWidget(int nParentID, QWidget *parent)
    : QWidget{parent}
    ,m_nParentID(nParentID)
    ,m_pSubCmdWidget(NULL)
{
    this->setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    createBtns();
    setStyleSheet("QWidget{background-color: transparent; };");
    qApp->installEventFilter(this);
}

UpperLimbWidget::~UpperLimbWidget()
{
    qApp->removeEventFilter(this);
}

void UpperLimbWidget::createBtns()
{  
    QGridLayout * pLayout = new QGridLayout(this);
    pLayout->setMargin(0);
    pLayout->setSpacing(10);
    QList<Command> lsCmd = SysSettingModel::getInstance()->upperLimbCmdList(m_nParentID);
    QButtonGroup * pGroup = new QButtonGroup(this);
    int nSelectCmdID = 0;
    int nButtonCount = 0;
    for(int i = 0; i < lsCmd.size(); ++i)
    {
        if((lsCmd)[i].strShowText.trimmed() == "")
           continue;
        TouchButton * pBtn = new TouchButton(this);
        pBtn->setText((lsCmd)[i].strShowText);
        pBtn->setStyleSheet(QString("     QPushButton { \
        background-color: #66000000;  \
        color: white;  \
        border: 1px  solid  white; /* 边框宽度为1px，颜色为#A0A0A0 */  \
        border-radius: 0px; /* 边框圆角 */  \
        font: %1px \"微软雅黑\";  \
        } \
        QPushButton:pressed { \
        background-color: #00C8FF; \
        }\
        QPushButton:checked { \
        background-color: #00C8FF; \
        }").arg(int(24*g_xScale)));
        pBtn->setFixedSize(int(180*g_xScale), int(60*g_yScale));
        pLayout->addWidget(pBtn, nButtonCount / 4, nButtonCount % 4);
        pBtn->setCheckable(true);
        connect(pBtn,&QPushButton::clicked,this,[=](){
           Robot * pRobot = RobotList::getInstance()->getCurRobot();
           if(pRobot)
           {
              pRobot->setUpperCmd(lsCmd[i]);
           }
           //是否有子菜单
           QList<Command> lsSubCmd = SysSettingModel::getInstance()->upperLimbCmdList(lsCmd[i].nID);
           if(lsSubCmd.size() == 0)
               this->deleteLater();
           else
              showSubCmdWidget(lsCmd[i].nID);
        });
        Robot* pRobot = RobotList::getInstance()->getCurRobot();
        if(pRobot)
        {
            int nCmdID = pRobot->nUpperCmdID();
            while(nCmdID > 0)
            {
                if(nCmdID == lsCmd[i].nID)
                {
                    pBtn->setChecked(true);
                    nSelectCmdID = nCmdID;
                    break;
                }
                Command * pCmd = SysSettingModel::getInstance()->getUpperLimbCmd(nCmdID);
                nCmdID = 0;
                if(pCmd)
                {
                    nCmdID = pCmd->nParentID;
                }
            }
        }
        pGroup->addButton(pBtn);
        nButtonCount++;
    }

     // 根据按钮数量调整窗口大小
     int rowCount = (nButtonCount + 3) / 4; // 每行4个按钮
     int nColCount = nButtonCount > 3?4:nButtonCount;
     int width = nColCount * int(180*g_xScale) + (nColCount - 1) * 10 ;
     int height = rowCount * int(60*g_yScale) + (rowCount - 1) * 10;

     QFrame * pLine = new QFrame(this);
     pLine->setFixedSize(width,1);
     pLine->setStyleSheet("background-color:white");
     pLayout->addWidget(pLine,rowCount,0);
     QWidget * p = new QWidget(this);
     pLayout->addWidget(p,rowCount+1,0);
     setFixedSize(width, height+20);
     QTimer::singleShot(1,[=](){
         if(nSelectCmdID > 0)
         {
         QList<Command> lsSubCmd = SysSettingModel::getInstance()->upperLimbCmdList(nSelectCmdID);
             if(lsSubCmd.size() > 0 )
            showSubCmdWidget(nSelectCmdID);
         }
     });
}

void UpperLimbWidget::showSubCmdWidget(int nCurID)
{
    m_pSubCmdWidget = new UpperLimbWidget(nCurID,this);
    int centerX = width()/2;
    QPoint pt = QPoint(centerX- m_pSubCmdWidget->width()/2, 0 - m_pSubCmdWidget->height());
    m_pSubCmdWidget->move(mapToGlobal(pt));
    m_pSubCmdWidget->show();
}

bool UpperLimbWidget::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::ActivationChange)
    {
        if(QApplication::activeWindow() != this && !this->findChildren<QWidget*>().contains(QApplication::activeWindow()))
        {
            this->deleteLater();
        }
    }
    return QWidget::eventFilter(watched,event);
}

void UpperLimbWidget::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    this->deleteLater();
}


